function isUrl(url: string) {
	const re =
		/^(?:ws(s)?:\/\/)?[\w.-]+(?:\.[\w\.-]+)+[\w\-\._~:/?#[\]@!\$&'\(\)\*\+,;=.]+$/;
	return re.test(url);
}

export default function urlCheck(url: string) {
	// 初始化数据
	const tmpTag = document.location.protocol === "https:" ? "wss" : "ws";
	let host = window.location.host;
	let path = url;

	// 判断是否有ws或者http
	let urlList = url.split("/");
	if (
		urlList[0] === "wss:" ||
		urlList[0] === "ws:" ||
		urlList[0] === "http:" ||
		urlList[0] === "https:"
	) {
		host = urlList[2];
		path = "/" + urlList.slice(3).join("/");
	}

	// 判断是不是带域名
	if (isUrl(urlList[0])) {
		host = urlList[0];
		path = "/" + urlList.slice(1).join("/");
	}

	// 最后拼接
	const newUrl = `${tmpTag}://${host}${path}`;

	return newUrl;
}
